﻿using System;
using System.Collections;
using System.Collections.Generic;
using Me.Prettyprint.Cassandra.Connection;
using Me.Prettyprint.Cassandra.Serializers;
using Me.Prettyprint.Cassandra.Service;
using Me.Prettyprint.Cassandra.Utils;
using Me.Prettyprint.Hector.Api;
using Me.Prettyprint.Hector.Api.Beans;
using Me.Prettyprint.Hector.Api.Query;
using Org.Apache.Cassandra.Thrift;

namespace Me.Prettyprint.Cassandra.Model
{
    /// <summary>
    /// OrderedCounterRowsImpl extends CounterRowsImpl and implements OrderedCounterRows.
    /// The counter rows are returned in the order defined by the query.
    /// </summary>
    /// <typeparam name="K"></typeparam>
    /// <typeparam name="N"></typeparam>
    public class OrderedCounterRowsImpl<K, N> : CounterRowsImpl<K, N>, OrderedCounterRows<K, N>
    {
        public OrderedCounterRowsImpl(Dictionary<K, List<CounterColumn>> thriftRet, Serializer<N> nameSerializer) : base(thriftRet, nameSerializer)
        {
            RowsList = new List<CounterRow<K, N>>(Rows.Values);
        }
        protected IEnumerator<CounterRow<K, N>> Enumerator
        {
            get;
            set;
        }
        protected List<CounterRow<K, N>> RowsList
        {
            get;
            set;
        }
        public List<CounterRow<K,N>> List
        {
            get
            {
                return RowsList;
            }
        }
        public CounterRow<K, N> PeekLast()
        {
            return RowsList != null && RowsList.Count > 0 ? RowsList[RowsList.Count -1] : null;
        }
        public new IEnumerator GetEnumerator()
        {
            if (Enumerator == null)
            {
                Enumerator = RowsList.GetEnumerator();
            }
            return Enumerator;
        }
    }
}
